package com.asdan.seededu.web.controller;

import com.asdan.seededu.util.Check;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.asdan.seededu.entity.security.SubProject;
import com.asdan.seededu.entity.security.User;
import com.asdan.seededu.front.service.ISWEEPService;
import com.asdan.seededu.front.service.WyefService;
import com.asdan.seededu.front.service.impl.ISWEEPServiceImp;
import com.asdan.seededu.front.service.impl.WyefServiceImp;
import com.asdan.seededu.service.OrderService;
import com.asdan.seededu.service.impl.OrderServiceImpl;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;

@Controller
@MenuSetting("menu-user")
@RequestMapping("order")
public class OrderController {
	
	
	@RequestMapping(value="/bm",method=RequestMethod.GET)
	public String bm(Model model,String spid ,String message ,HttpServletRequest req){
				WyefService wf = new WyefServiceImp() ;
				SubProject sp = wf.getNewWyef() ;
				spid=Integer.toString(sp.getSpid());
				//通过session获取用户id,获取service函数
				String uid=(String) req.getSession().getAttribute("userid");
		
				//判断uid是否合法
				if(uid==null || "".equals(uid)){
					return "redirect:/sign_In.jsp";
				}
				if(spid==null || "".equals(spid)){
					return "redirect:/index.jsp";
				}
				if(wf.HasOrder(Integer.parseInt(uid),Integer.parseInt(spid))){
					
					//有订单存在
					model.addAttribute("alreadyMessage","yes");
					return "index_Wyef";
				}
				else
				{
					return order(model,message,req);
				}
	}
	
	@RequestMapping(value="/wyeforder",method=RequestMethod.GET)
	public String order(Model model,String message ,HttpServletRequest req){
		String uid =(String)req.getSession().getAttribute("userid") ;
		if(uid!=null&&!"".equals(uid)){
			WyefService wf = new WyefServiceImp() ;
			SubProject sp = wf.getNewWyef() ;
			//String spid=(String) req.getSession().getAttribute("spid");
			//String fpid=(String) req.getSession().getAttribute("fpid");
			model.addAttribute("sp",  sp) ;
			if(message!=null&&!"".equals(message)){
				if("no".equals(message))
					message = "支付失败!" ;
				model.addAttribute("message",message) ;
			}
			OrderService ss = new OrderServiceImpl() ;
			User stu = ss.getOrderInfoByID(Integer.parseInt(uid),sp.getSpid(),sp.getFpid()) ;
			model.addAttribute("stu",stu);
			HttpSession session = req.getSession();
			session.setAttribute("stu",stu);
			model.addAttribute("stu",stu);
			return "pay/order";
		}else{
			return "redirect:/sign_In.jsp" ;
		}
	}
	@RequestMapping(value="/pay",method=RequestMethod.POST)
	public String pay(String name,String sex,String firstName ,String lastName,
			String schoolname,String schoolId,String grade,String email,String cellPhone,
			String idCard,String fatherName,String fatherEmail,String fatherPhone,
			String motherName,String motherEmail,String motherPhone,
			String RadioGroupKnow1,String RadioGroupAgree1,String RadioGroupHotel1,String spid,String fpid,String price,String projectname,String startTime,Model model,HttpServletRequest req){
		String uid=(String) req.getSession().getAttribute("userid");
		model.addAttribute("uid", uid);
		model.addAttribute("name", name);
		model.addAttribute("sex", sex);
		model.addAttribute("firstName", firstName);
		model.addAttribute("lastName", lastName);
		model.addAttribute("schoolname", schoolname);
		model.addAttribute("grade", grade);
		model.addAttribute("email", email);
		model.addAttribute("cellPhone", cellPhone);
		model.addAttribute("idCard", idCard);
		model.addAttribute("fatherName", fatherName);
		model.addAttribute("fatherEmail", fatherEmail);
		model.addAttribute("fatherPhone", fatherPhone);
		model.addAttribute("motherName", motherName);
		model.addAttribute("motherEmail", motherEmail);
		model.addAttribute("motherPhone", motherPhone);
		model.addAttribute("ifknow", RadioGroupKnow1);
		model.addAttribute("ifagree", RadioGroupAgree1);
		model.addAttribute("ifhotel", RadioGroupHotel1);
		model.addAttribute("price", price);
		model.addAttribute("schoolId", schoolId);
		model.addAttribute("spid", spid);
		model.addAttribute("fpid", fpid);
		model.addAttribute("projectname", projectname);
		model.addAttribute("startTime", startTime);
		return "pay/pay";

	}
	@RequestMapping(value="/pay_update_json",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	public @ResponseBody String update_json(Model model,HttpServletRequest req){
		//System.out.println(stu.getName());
		//System.out.println(stu.getSex());
		//HttpSession session = req.getSession();

		String uid=(String) req.getSession().getAttribute("userid");
		System.out.println("订单提交时用户id："+uid);

		//String uid=req.getParameter("uid");
		String name=req.getParameter("name");
		String sex=req.getParameter("sex");
		String schoolname=req.getParameter("schoolname");
		String schoolId=req.getParameter("schoolId");
		String grade=req.getParameter("grade");
		String QQ=req.getParameter("QQ");
		String email=req.getParameter("email");
		String cellPhone=req.getParameter("cellPhone");
		String idCard=req.getParameter("idCard");
		String fatherName=req.getParameter("fatherName");
		String fatherEmail=req.getParameter("fatherEmail");
		String fatherPhone=req.getParameter("fatherPhone");
		String motherName=req.getParameter("motherName");
		String motherEmail=req.getParameter("motherEmail");
		String motherPhone=req.getParameter("motherPhone");
		String ifknow=req.getParameter("ifknow");
		String ifagree=req.getParameter("ifagree");
		String firstName=req.getParameter("firstName");
		String lastName=req.getParameter("lastName");
		String ifhotel=req.getParameter("ifhotel");
		String price=req.getParameter("price");
		String spid=req.getParameter("spid");
		String fpid=req.getParameter("fpid");
		String temp = this.check_form(name, sex, firstName, lastName, schoolname, grade, email, cellPhone, idCard, fatherName, fatherEmail, fatherPhone, motherName, motherEmail, motherPhone, ifknow, ifagree, ifhotel, price, model);
		if(!"".equals(temp)){
			model.addAttribute("message_error", "数据有误错误,请重新提交订单！");
			System.out.println(req.getSession().getServletContext().getContextPath());
			return "<html><head><title>数据提交错误</title></head><body><script language='javascript'>document.location = '"+req.getSession().getServletContext().getContextPath()+"/order/wyeforder?message="+temp+"'</script></body></html>";
		}

		Map <String,String> hashmap=new HashMap <String,String>();
		hashmap.put("uid",uid);
		hashmap.put("name",name);
		hashmap.put("sex",sex);
		hashmap.put("grade",grade);
		hashmap.put("email",email);
		hashmap.put("QQ",QQ);
		hashmap.put("cellPhone",cellPhone);
		hashmap.put("idCard",idCard);
		hashmap.put("schoolname",schoolname);
		hashmap.put("schoolId",schoolId);
		hashmap.put("fatherName", fatherName);
		hashmap.put("fatherEmail",fatherEmail );
		hashmap.put("fatherPhone",fatherPhone);
		hashmap.put("motherName",motherName);
		hashmap.put("motherEmail",motherEmail);
		hashmap.put("motherPhone", motherPhone);
		hashmap.put("ifknow",ifknow);
		hashmap.put("ifagree",ifagree);
		hashmap.put("firstName",firstName);
		hashmap.put("lastName", lastName);
		hashmap.put("spid",spid);
		hashmap.put("fpid", fpid);
		String information=JsonUtils.toJsonString(JSONArray.toJSON(hashmap));
		System.out.println(information+"data++++++++++");
		String orders_id = "1";
		//int spid=1;//子项目id
		//int fpid=1;//父项目id
		//String price="1999";//项目价格
		OrderService ss = new OrderServiceImpl();
		System.out.println(Integer.parseInt(spid));
		System.out.println(Integer.parseInt(fpid));
		System.out.println(price);
		if(ss.getPriceById(Integer.parseInt(spid),Integer.parseInt(fpid),price)==false){
			model.addAttribute("message", "订单提交失败，价格错误！");
		}else{
			//boolean temp_update = ss.updateUser(stu,Integer.parseInt(uid));
			boolean temp_json=ss.addOrders(Integer.parseInt(uid),Integer.parseInt(spid),Integer.parseInt(fpid),price,information);
			//model.addAttribute("stu",new StudentServiceImpl().getStudentByEmailOrID("", stu.getSuid())) ;
			if(temp_json){
				model.addAttribute("message", "订单提交成功！") ;
				orders_id = ss.getOrdersId(Integer.parseInt(uid),Integer.parseInt(spid));
			}else{
				model.addAttribute("message", "订单提交失败") ;
			}
		}
		//ModelAndView view = new ModelAndView();
		//view.setViewName("redirect:http://localhost:8090/seededu/index.jsp");
		//return view;
		//订单提交成功后进入支付
		String body = "耶鲁经济论坛支付body";
		String subject = "耶鲁经济论坛支付";
		String show_url="http://www.seededu.cn/index.jsp";
		String receive_name = name;
		String receive_address = "北京市海淀区";
		String receive_zip = "100011";
		String receive_phone = cellPhone;
		String receive_mobile = cellPhone;
		String out_trade_no = uid;
		//uid = "1";
		return ss.AddAlipay(orders_id,uid, out_trade_no, subject, price, body, show_url, receive_name, receive_address, receive_zip, receive_phone, receive_mobile);
	}

	@RequestMapping(value="/repay",method=RequestMethod.GET,produces="text/html;charset=UTF-8")
	public @ResponseBody String repay_order(Model model,HttpServletRequest req){
		OrderService ss = new OrderServiceImpl();
		String orders_id = req.getParameter("oid");
		List<HashMap> list = ss.getOrder(orders_id);
		HashMap hm = (HashMap) list.get(0);
		//订单提交成功后进入支付
		String body = hm.get("body").toString();
		String subject = hm.get("subject").toString();
		String show_url= hm.get("show_url").toString();
		String receive_name = hm.get("receive_name").toString();
		String receive_address = hm.get("receive_address").toString();
		String receive_zip = hm.get("receive_zip").toString();
		String receive_phone = hm.get("receive_phone").toString();
		String receive_mobile = hm.get("receive_mobile").toString();
		String out_trade_no = hm.get("out_trade_no").toString();
		String price = hm.get("price").toString();
		String uid = hm.get("uid").toString();
		//uid = "1";
		return ss.AddAlipay(orders_id,uid, out_trade_no, subject, price, body, show_url, receive_name, receive_address, receive_zip, receive_phone, receive_mobile);
	}
	public String check_form(String name,String sex,String firstName ,String lastName,
			String schoolname,String grade,String email,String cellPhone,
			String idCard,String fatherName,String fatherEmail,String fatherPhone,
			String motherName,String motherEmail,String motherPhone,
			String ifknow,String ifagree,String ifhotel,String price,Model model) {
		String temp="";
		if(!Check.checkNumber(price)){
			temp = "价格不是数字" ;
			return temp;
		}
		/*if( (ifhotel.equals("是") && Integer.parseInt(price)!=2800) 
				||
			(ifhotel.equals("否") && Integer.parseInt(price)!=1800) 
				) {
			temp =  "价格类型不正确！" ;
			return temp;
		}*/

		if(!Check.checkEmail(email)){
			temp = "邮箱不正确！";
			return  temp;
		}

		if(cellPhone==null||"".equals(cellPhone)||cellPhone.length()!=11){
			temp = "手机号不正确！";
			return temp;
		}
		if(!Check.checkIdCard(idCard)||idCard==null||"".equals(idCard)){
			temp =  "身份证号不正确！";
			System.out.println("4");
			return temp;
		}
		if(!(
				(fatherName!=null&&!"".equals(fatherName) 
				&& fatherEmail!=null&&!"".equals(fatherEmail)
				&&fatherPhone!=null&&!"".equals(fatherPhone))||
				(motherName!=null&&!"".equals(motherName)
				&&motherName!=null&&!"".equals(motherName)
				&&motherName!=null&&!"".equals(motherName))
				)){
			temp =  "请将填全父母联系方式至少填全一个！" ;
			return temp ;
		}
		if(((fatherName!=null&&!"".equals(fatherName) 
				&& fatherEmail!=null&&!"".equals(fatherEmail)
				&&fatherPhone!=null&&!"".equals(fatherPhone))
				)){
			if(!Check.checkEmail(fatherEmail)){
				temp =  "父亲邮箱不正确！"  ;
				return temp;
			}
			if(fatherPhone.length()!=11){
				temp =  "父亲手机号不正确！"  ;
				return temp ;
			}

		}
		if(((motherName!=null&&!"".equals(motherName)
				&&motherEmail!=null&&!"".equals(motherEmail)
				&&motherPhone!=null&&!"".equals(motherPhone))
				)){
			if(!Check.checkEmail(motherEmail)){
				temp =  "母亲邮箱不正确！"  ;
				return temp ;
			}

			if(motherPhone.length()!=11){
				temp =  "母亲手机号不正确！" ;
				return temp;
			}
		}
		if(ifknow==null||"".equals(ifknow)){
			temp =  "请选择父母是否已知晓相关项目信息！"  ;
			return temp ;
		}
		if(ifagree==null||"".equals(ifagree)){
			temp = "父母是否已同意参加该项目！" ;
			return temp;
		}
		if(ifhotel==null||"".equals(ifhotel)){
			temp =  "是否选择住宿！"  ;
			return temp;
		}


		return temp;
	}
	
	
	//ISWEEP
	
	@RequestMapping(value="/isweepbm",method=RequestMethod.GET)
	public String isweepbm(Model model,String spid ,String message ,HttpServletRequest req){
		ISWEEPService is=new ISWEEPServiceImp();
				SubProject sp = is.getNewISWEEP();
				spid=Integer.toString(sp.getSpid());
				//通过session获取用户id,获取service函数
				String uid=(String) req.getSession().getAttribute("userid");
		
				//判断uid是否合法
				if(uid==null || "".equals(uid)){
					return "redirect:/sign_In.jsp";
				}
				if(spid==null || "".equals(spid)){
					return "redirect:/index.jsp";
				}
				if(is.HasOrder(Integer.parseInt(uid),Integer.parseInt(spid))){
					
					//有订单存在
					model.addAttribute("alreadyMessage","yes");
					return "index_ISWEEP";
				}
				else
				{
					return isweeporder(model,message,req);
				}
	}
	
	@RequestMapping(value="/isweeporder",method=RequestMethod.GET)
	public String isweeporder(Model model,String message ,HttpServletRequest req){
		String uid =(String)req.getSession().getAttribute("userid") ;
		if(uid!=null&&!"".equals(uid)){
			ISWEEPService is=new ISWEEPServiceImp();
			SubProject sp = is.getNewISWEEP() ;
			model.addAttribute("sp",  sp) ;
			if(message!=null&&!"".equals(message)){
				if("no".equals(message))
					message = "支付失败!" ;
				model.addAttribute("message",message) ;
			}
			OrderService ss = new OrderServiceImpl() ;
			User stu = ss.getOrderInfoByID(Integer.parseInt(uid),sp.getSpid(),sp.getFpid()) ;
			model.addAttribute("stu",stu);
			HttpSession session = req.getSession();
			session.setAttribute("stu",stu);
			model.addAttribute("stu",stu);
			return "pay/order";
		}else{
			return "redirect:/sign_In.jsp" ;
		}
	}
}
